package cn.edu.zafu.bigdata.lwg;

import java.io.*;
import java.util.*;

public class StudentSystem {
    private static final String FILE_NAME = "students.dat";
    private static List<Student> studentList = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    // 学生类
    public static class Student implements Serializable {
        private static final long serialVersionUID = 1L;
        private String id;
        private String name;
        private String phone;

        public Student(String id, String name, String phone) {
            this.id = id;
            this.name = name;
            this.phone = phone;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", phone='" + phone + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        loadStudentsFromFile();
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    retrieveStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    saveStudentsToFile();
                    System.out.println("退出程序。");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的输入，请重新输入。");
                    break;
            }
        }
    }

    private static void addStudent() {
        System.out.print("请输入学生学号: ");
        String id = scanner.nextLine();
        for (Student student : studentList) {
            if (student.getId().equals(id)) {
                System.out.println("学号重复，请重新输入。");
                return;
            }
        }
        System.out.print("请输入学生姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入学生电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        studentList.add(student);
        saveStudentsToFile();
        System.out.println("学生信息添加成功。");
    }

    private static void retrieveStudent() {
        while (true) {
            System.out.print("请输入要检索的学生学号（输入QUIT退出）: ");
            String input = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(input)) {
                return;
            }
            for (Student student : studentList) {
                if (student.getId().equals(input)) {
                    System.out.println(student);
                    return;
                }
            }
            System.out.println("未找到该学生。");
        }
    }

    private static void updateStudent() {
        while (true) {
            System.out.print("请输入要更新的学生学号（输入QUIT退出）: ");
            String input = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(input)) {
                return;
            }
            for (Student student : studentList) {
                if (student.getId().equals(input)) {
                    System.out.print("请输入新的姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入新的电话: ");
                    String phone = scanner.nextLine();
                    student.setName(name);
                    student.setPhone(phone);
                    saveStudentsToFile();
                    System.out.println("学生信息更新成功。");
                    return;
                }
            }
            System.out.println("未找到该学生。");
        }
    }

    private static void deleteStudent() {
        while (true) {
            System.out.print("请输入要删除的学生学号（输入QUIT退出）: ");
            String input = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(input)) {
                return;
            }
            boolean removed = studentList.removeIf(student -> student.getId().equals(input));
            if (removed) {
                saveStudentsToFile();
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void loadStudentsFromFile() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentList = (List<Student>) ois.readObject();
        } catch (EOFException e) {
            studentList = new ArrayList<>();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void saveStudentsToFile() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(studentList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}